// let BASE_URL = "https://wlsy.nanjingyunpeng.cn";
// let BASE_URL = "https://sssx.nanjingyunpeng.cn/index.php";
let BASE_URL = "https://www.dayige.cc/api";

// #ifdef H5
// BASE_URL = "/api";
// #endif
const request = (obj, data, loading) => {
	if (loading) uni.showLoading({
		title: "加载中",
		mask: true
	});
	let Authorization = uni.getStorageSync("token") || ""
	let httpDefaultOpts = {
		url: BASE_URL + obj.url,
		data: data,
		beforeSend: function(xmlHttp) {
			xmlHttp.setRequestHeader("If-Modified-Since", "0");
			xmlHttp.setRequestHeader("Cache-Control", "no-cache");
		},
		method: obj.method || 'post',
		dataType: "json",
		header: {
			"Content-type": "application/json",
		},
	};
	if (Authorization) {
		if (httpDefaultOpts.header) {
			// console.log("1111")
			httpDefaultOpts.header = {
				Authorization: "Bearer " + Authorization
			};
		} else {
			// console.log("2222")
			httpDefaultOpts.header["Authorization"] = Authorization;
		}
	} else {
		// console.log("获取不到userToken");
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts)
			.then((res) => {
				// console.log("res,res", res)
				if (loading) uni.hideLoading();
				if (res.statusCode == 200) {
					resolve(res.data);
				} else if (res.data.code == 1005 || res.data.code == 1001) {
					uni.showToast({
						title: res.data.message,
						icon: "none",
						duration: 1500
					});
					uni.removeStorageSync("token")
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/selectIdentity"
						});
					}, 2000)
					return
				}else {
					reject(res);
				}

			})
			.catch((response) => {
				reject(response);
			});
	});
	return promise;
};
export default {
	BASE_URL,
	request,
};